home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Source / MiscKit / MiscString_ExtendedParsing.m < prev    next >
Encoding:
Text File  |  1995-07-06  |  3.4 KB  |  134 lines

  1. //
  2. //    MiscString_ExtendedParsing.m -- substrings with token elimiters
  3. //        Written by Thomas Engel Copyright (c) 1994 by Thomas Engel.
  4. //                Version 1.00.  All rights reserved.
  5. //        This notice may not be removed from this source code.
  6. //
  7. //    This object is included in the MiscKit by permission from the author
  8. //    and its use is governed by the MiscKit license, found in the file
  9. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  10. //    for a list of all applicable permissions and restrictions.
  11. //    
  12.  
  13. #import <misckit/MiscString.h>
  14.  
  15. @implementation MiscString(ExtendedParsing)
  16.  
  17. - substringFromToken:(int)start toToken:(int)end ofList:aList
  18. {
  19.     // We want to get the substring between two tokens of the tokenList.
  20.     // If there is no tokenlist we will do a standard tokenization.
  21.     // If you pass a wrong list and wrong numbers you might get wrong
  22.     // results!
  23.     
  24.     BOOL    mustFreeList = NO;
  25.     int        i;
  26.     id         substring;
  27.     id        aString;
  28.     id        token;
  29.     
  30.     if( !aList )
  31.     {
  32.         mustFreeList = YES;
  33.         aList = [self tokenize:" \t\n\r" into:nil];
  34.     }
  35.     // Now check the token numbers. we could return nil in these cases but
  36.     // We'll try to find something 'useful'.
  37.  
  38.     if( start > end )
  39.     {
  40.         i = start;
  41.         start = end;
  42.         end = i;
  43.     }
  44.     if( end > [aList count]-1 ) end = [aList count]-1;
  45.     if( end < 0 ) end = 0;
  46.     if( start > [aList count]-1 ) start = [aList count]-1;
  47.     if( start < 0 ) start = 0;
  48.     
  49.     // Ok. from what pos to what pos does our substring range.
  50.     // Lets reduce it step by step from the edges.
  51.     // If the token List has no items at all we'll do nothing
  52.     
  53.     substring = [self copy];
  54.     if( [aList count] != 0 )
  55.     {
  56.         for( i=0; i<start+1; i++ )
  57.         {
  58.             // We will try to remove every token up to the wanted.
  59.             // But we will have to remove leading spaces etc. too.
  60.         
  61.             token = [aList objectAt:i];
  62.             do
  63.             {
  64.                 if( [substring length] < 1 ) break;
  65.         
  66.                 if( [substring compareTo:token 
  67.                          n:[token length] caseSensitive:YES] == 0 ) break;
  68.             
  69.                 // Seems like the substing does not start with the wanted token
  70.                 // So lets remove one char and try it again.
  71.             
  72.                 aString = [substring midFrom:1 to:[substring length]-1];
  73.                 [substring free];
  74.                 substring = aString;
  75.             }
  76.             while( YES );
  77.  
  78.             // If this is not the token we are searching for...remove it.
  79.         
  80.             if( i != start )
  81.             {
  82.                 aString = [substring midFrom:[token length] 
  83.                                         to:[substring length]-1];
  84.                 [substring free];
  85.                 substring = aString;
  86.             }
  87.         }
  88.     
  89.         // Now lets kill the unimportant stuff to the right...
  90.         // No big deal same as before.
  91.     
  92.         for( i=[aList count]-1; i>end-1; --i )
  93.         {
  94.             // We will try to remove every token up to the wanted.
  95.             // But we will have to remove leading spaces etc. too.
  96.         
  97.             token = [aList objectAt:i];
  98.             do
  99.             {
  100.                 if( [substring length] < 1 ) break;
  101.         
  102.                 if( [substring endCompareTo:token n:[token length]
  103.                          caseSensitive:YES] == 0 ) break;
  104.             
  105.                 // Seems like the substing does not start with the wanted token
  106.                 // So lets remove one char and try it again.
  107.             
  108.                 aString = [substring midFrom:0 to:[substring length]-2];
  109.                 [substring free];
  110.                 substring = aString;
  111.             }
  112.             while( YES );
  113.  
  114.             // If this is not the token we are searching for...remove it.
  115.             
  116.             if( i != end )
  117.             {
  118.                 aString = [substring midFrom:0
  119.                                 to:[substring length] - [token length] - 1];
  120.                 [substring free];
  121.                 substring = aString;
  122.             }
  123.         }        
  124.     }
  125.  
  126.     // Now lets free the list if needed.
  127.  
  128.     if( mustFreeList ) [aList free];
  129.     
  130.     return substring;
  131. }
  132.  
  133. @end
  134.